import React from 'react';
import styles from './GoodsTable.css';
import GoodsItem from './GoodsItem';
import {pageSize} from './GoodsPagger.js';
let getCurPage = (curPage,totalPage)=>{
  // console.log('curPage1=' + curPage)
  if(curPage<0){
    curPage=0
  }
  if(curPage >= totalPage){
    curPage=totalPage-1
  }
  // console.log('curPage2=' + curPage)
  return curPage
}
let getCurPageGoodsList =(curPage,goodsList)=>{
  // console.log('curPage3=' + curPage)
  // console.log(goodsList)
  let startIndex=curPage*pageSize
  let curPageGoodsList =[]
  // console.log(curPageGoodsList)
  for (let i=0;i<pageSize;i++){
    let curIndex = startIndex+i
    if (goodsList[curIndex]){
      curPageGoodsList.push(goodsList[curIndex])
    }
  }
  // console.log(curPageGoodsList)
  return curPageGoodsList
}

function GoodsTable(props) {
  let goodsList = props.goods.content
  // console.log('goodsList.length=' + goodsList.length)
  let totalPage = Math.ceil(goodsList.length/pageSize)
  let curPage = getCurPage(props.goods.curPage,totalPage)
  let curPageGoodsList = getCurPageGoodsList(curPage,goodsList)
  return (
    <table className={styles.normal}>
      <tr>
        <td>id</td>
        <td>isbn</td>
        <td>title</td>
        <td>prise</td>
        <td>操作</td>
      </tr>
      {
        curPageGoodsList.map((goods)=>{
          return (
            <GoodsItem {...goods} {...props} key={goods.id}/>
          )
        })
      }
    </table>
  );
}

export default GoodsTable;
